package com.why.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.why.dao.UserMapper;
import com.why.pojo.User;
import com.why.service.UserService;
import com.why.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean deleteByPrimaryKey(Integer id) {
        if (userMapper.deleteByPrimaryKey(id)==1){
            return true;
        }
        return false;
    }

    @Override
    public boolean insertSelective(User record) {
        if (userMapper.insertSelective(record)==1){
            return true;
        }
        return false;
    }

    @Override
    public User selectByPrimaryKey(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean updateByPrimaryKeySelective(User record) {
        if (userMapper.updateByPrimaryKeySelective(record)==1){
            return true;
        }
        return false;
    }

    @Override
    public User findUserByName(User user) {
        return userMapper.findUserByName(user);
    }

    @Override
    public PageInfo<User> getPageInfo(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> list = userMapper.selectAllUser();
        PageInfo<User> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public boolean updatePassword(Integer id) {
        User user = userMapper.selectByPrimaryKey(id);
        user.setPassword(SecurityUtils.md5Hex3("666666"));
        if (userMapper.updatePassword(user)==1){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateImag(User user) {
        if (userMapper.updateImag(user)==1){
            return true;
        }
        return false;
    }


}
